package listbyorder.access201_300.test239;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/8/20 14:56
 */
public class Solution3 {

    // 超时
    public int[] maxSlidingWindow(int[] nums, int k) {
        if (nums == null || nums.length == 0 || k <= 0) return null;
        int n = nums.length;
        int[] res = new int[n - k + 1];

        // 创建一个优先级队列
        PriorityQueue<Integer> queue = new PriorityQueue<Integer>(new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        int c = 0;
        int index = 0;
        for (; c < k; c++) {
            queue.add(nums[c]);
        }
        while (c < n) {
            res[index++] = queue.peek();
            queue.add(nums[c]);
            queue.remove(nums[c - k]);
            c++;
        }
        res[index++] = queue.peek();
        return res;
    }
}
